var pages = 1;
var tpnum = 0;
$(function() {
	
	if(getCookieValue("token")==""){
		window.location.href = 'l-userlogin.html'
	}
	
	
	
	
	collectionUp();
	//收藏遍历
	collectionList({
		"pages": 1,
		"sort": '',
		"userUuid":CD.geturl().business
		
	}, function(data) {
		$('.mui-table-view').html(template('buyCollection', data));
		//点击跳转
		collectionClick();
		//删除收藏
		collectionDelete();
	});

	//排序点击
	$('.ld_order a').on('tap', function() {
		var $this = $(this);
		if(!$this.hasClass('now')) {
			$this.addClass('now').siblings().removeClass('now')
				.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
		}
		else {
			if($this.find('span').hasClass('fa-angle-down')) {
				$this.find('span').removeClass('fa-angle-down').addClass('fa-angle-up');
			} else {
				$this.find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
			}
		}
	});

	//价格排序
	$('#price-collection').on('tap', function(){
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0,0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			tpnum = 2;
			collectionList({
				"pages": 1,
				"sort": tpnum,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyCollection', data));
				collectionDelete();
				pages = 1;
				collectionUp(pages);
				collectionClick();
			});
		} else {
			tpnum = 3;
			collectionList({
				"pages": 1,
				"sort": tpnum,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyCollection', data));
				pages = 1;
				collectionDelete();
				collectionUp(pages);
				collectionClick();
			});
		}
	});
	
	//时间排序
	$('#time-collection').on('tap', function() {
		mui('#mui-scroll-wrapper').pullRefresh().refresh(true);
		mui('#mui-scroll-wrapper').scroll().setTranslate(0,0);
		if($(this).hasClass('now') && $(this).children().hasClass('fa-angle-down')) {
			tpnum = 0;
			collectionList({
				"pages": 1,
				"sort": tpnum,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyCollection', data));
				collectionDelete();
				pages = 1;
				collectionUp(pages);
				collectionClick();
			});
		} else {
			tpnum = 1;
			collectionList({
				"pages": 1,
				"sort": tpnum,
				"userUuid":CD.geturl().business
			}, function(data) {
				$('.mui-table-view').html(template('buyCollection', data));
				pages = 1;
				collectionDelete();
				collectionUp(pages);
				collectionClick();
			});
		}
	});


});

//收藏遍历
var collectionList = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/goods/findCollectBayUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//删除收藏接口
var collectDelet = function(num, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/collect/deleteBayGoodsUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: num,
		dataType: 'json',
		success: function(data) {
			console.log(data, 9999);
			callback && callback(data);
		}
	});
}

var collectionUp = function() {
	mui.init({
		pullRefresh: {
			container: "#mui-scroll-wrapper",
			up: {
				callback: function() {
					var that = this;
					pages++;
					collectionList({
						"pages": pages,
						"sort": tpnum,
						"userUuid":CD.geturl().business
					}, function(data) {
						$('.mui-table-view').append(template('buyCollection', data));
						collectionClick();
						collectionDelete();
						if(data.rows.length) {
							that.endPullupToRefresh();
						} else {
							that.endPullupToRefresh(true);
						}
					})
				}
			}
		}
	});
}

//点击跳转
var collectionClick = function() {
	$('.mui-slider-handle').on('tap', function() {
		var productId = $(this).parent().attr('data-id');
		var business = $(this).attr('data-business');
		console.log(productId);
		window.location.href = 'v-shop-product.html?id=' + productId+'&business='+business;
	});
}

//删除封装
var collectionDelete = function() {
	$('.mui-btn').on('tap', function() {
		var goodsId = $(this).parent().parent('.mui-table-view-cell').attr('data-id');
		var deleteProduct = $(this).parent().parent('.mui-table-view-cell');
		console.log(goodsId);
		$('.buy-delete').stop().fadeIn();
		$('#buy-delete-close').click(function() {
			$('.buy-delete').stop().fadeOut();
			return false;
		});
		$('#buy-delete-open').click(function() {
			$('.buy-delete').stop().fadeOut();

			collectDelet({
				goodsId: goodsId,
				userUuid:CD.geturl().business
			}, function(data) {
				console.log('删除成功');
			});
			deleteProduct.stop().fadeOut(500);
		});

	});
}